<template>
  <div class="communityContainer">
    <div class="community">
      <div class="right">
        <div class="inter" v-loading="isDataLoad">
          <div class="interItem">
            <div class="ItemCenter">
              <div class="title">
                <h4>用户名</h4>
              </div>
            </div>
            <div class="ItemCenter">
              <h4>昵称</h4>
            </div>
            <div class="ItemCenter">
              <h4>邮箱</h4>
            </div>
            <div class="ItemCenter">
              <h4>电话</h4>
            </div>
            <div class="ItemCenter">
              <h4>状态</h4>
            </div>
          </div>
          <div class="interItem"
               v-for="(item, index) in userList"
               :key="index" :style="index%2==1?'background:#f5f7fa':''">
            <div class="ItemCenter"  @click="gotoPersonal(item.userId)">
              <div class="title">{{ item.userName }}</div>
              <div class="publishDate">
                {{ item.createTime | handleUsersDateShow }}
              </div>
            </div>
            <div class="ItemCenter"  @click="gotoPersonal(item.userId)">
              {{ item.nickName }}
            </div>
            <div class="ItemCenter"  @click="gotoPersonal(item.userId)">
              {{ item.email }}
            </div>
            <div class="ItemCenter" @click="gotoPersonal(item.userId)">
              {{ item.phoneNumber }}
            </div>
            <div class="ItemCenter" >
              <span v-if="item.status==1">禁用</span>
              <span v-if="item.status==0">可用</span>
            </div>
            <button class="delete" @click="deleteUser(item.userId,item.status)" :style="item.status==1?'background:#8ea8e5':''">
              <span v-if="item.status==1">解除</span>
              <span v-if="item.status==0">禁用</span>
            </button>
          </div>
        </div>
        <div class="nullTips" v-if="userList.length == 0 && isDataLoad == false">
          Users Not Found !
        </div>
        <div class="bottom" v-if="!isDataLoad && userList.length !== 0">
          <!-- 分页组件 -->
          <el-pagination
              background
              layout="prev, pager, next"
              :total="totalCount"
              :current-page="this.$route.query.page * 1"
              @current-change="changePage"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <!-- 返回顶部组件 -->
    <go-top></go-top>
  </div>
</template>

<script>

import {handleUsersDateShow} from "plugins/utils.js";
import GoTop from "components/goTop/GoTop.vue";

export default {
  name: "users",
  components: {
    GoTop,
  },
  data() {
    return {
      userList: [],
      // 总条数
      totalCount: 0,
      // 是否正在加载数据
      isDataLoad: true,
      userEdit: {
        userId: "",
        userName: "",
        status: "",
        phoneNumber: "",
      },
      cencle: false,
    };
  },
  methods: {
    // 跳转至个人主页
    gotoPersonal(id) {
      console.log("我被调用了"+id);
      this.$router.push({
        name: "personal",
        params: {id},
        query: {type: "post", page: 1},
      });
    },
    // 请求
    async getAllUsers() {
      this.isDataLoad = true;
      let res = await this.$request("/dquser/superlist", {
        pageNum: this.$route.query.page,
        pageSize: 10,
      });
      console.log("我是结果"+res);
      if (res.data.code == 200) {
        this.totalCount = res.data.data.total;
        this.userList = res.data.data.list;
        this.isDataLoad = false;
        // 返回顶部
        window.scrollTo({
          top: 0,
          behavior: "smooth",
        });
      }
    },

    async deleteUser(userId,status) {
      console.log(userId)
      console.log("我是状态"+status)
      if(status==0){
        this.$confirm("确认要禁用此用户吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
            .then(async () => {
              let res = await this.$request(
                  `/dquser/delete/`+userId
              );
              if (res.data.code == 200) {
                this.$message.success("禁用成功!");
                location.reload()
              }
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消禁用",
              });
            });
      }else{
        this.$confirm("确认要解禁此用户吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
            .then(async () => {
              let res = await this.$request(
                  `/dquser/delete/`+userId
              );
              if (res.data.code == 200) {
                this.$message.success("解禁成功");
                location.reload()
              }
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "已取消解禁",
              });
            });

      }
    },
    // 切换分页的回调
    changePage(e) {
      // console.log(e);
      // this.currentPage = e;
      this.$router.push({
        name: "users",
        query: {page: e},
      });
      this.getAllUsers();

    },
  },
  computed: {},
  filters: {
    handleUsersDateShow,
  },
  created() {
    this.getAllUsers();
  },
};
</script>

<style scoped>

.edit {
  height: 40px;
  background: #8ea8e5;
  color: white;
  border-radius: 7px;
  border: 1px solid #8ea8e5;
  width: 60px;
}

.delete {
  margin-left: 20px;
  width: 100px;
  height: 40px;
  background: red;
  color: white;
  border-radius: 7px;
  border: 1px solid #8ea8e5;
}

.communityContainer {
  display: flex;
  justify-content: center;
  font-size: 15px;
}

.community {
  display: flex;
  max-width: 1200px;
  width: 85vw;
}


.right {
  padding: 10px 20px 20px;
  width: calc(100%);
}

.interItem {
  display: flex;
  position: relative;
  border-bottom: 1px solid #eee;
  padding: 20px 20px 20px;
  cursor: pointer;
  border-radius: 5px;
  transition: all 0.15s;
}

.interItem:hover {
  background-color: #f2f6fb;
}

.userAvatar {
  width: 45px;
}

.userAvatar img {
  height: 40px;
  width: 40px;
  border-radius: 50%;
}

.ItemCenter {
  width: 460px;
  margin: 0 10px;
}

.ItemCenter div {
  margin-bottom: 1px;
  line-height: 18px;
}

.title {
  color: rgb(43, 43, 43);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.publishDate {
  color: rgb(136, 136, 136);
  margin: 4px 0;
  font-size: 12px;
}

.content {
  color: rgb(136, 136, 136);
  font-size: 14px;
  line-height: 19px;
  /* 两行溢出 */
  text-overflow: -o-ellipsis-lastline;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}

.interImg img {
  border-radius: 5px;
}


.interImgItem /deep/ .el-image__inner {
  width: unset;
}

.bottom {
  width: 100%;
  text-align: center;
  margin: 40px 0;
}

.communityContainer /deep/ .el-loading-spinner {
  margin-top: 80px;
}

.nullTips {
  text-align: center;
  margin-top: 20vh;
  color: #666;
  letter-spacing: 1px;
}
</style>
